package com.nativetalk.controller.app.setmeal;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.setmeal.TsNtSetmealIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class SetmealController {

    private static Logger logger = Logger.getLogger(SetmealController.class);

    /**
     * @return RetInfo
     * @Purpose 套餐列表
     * @version 1.0
     * @time 2015-01-05
     * @author lizhun
     */
    @RequestMapping(value = "/app/1/token/member/setmeal", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo setmeal() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo = new RetInfo();
            List<TsNtSetmealIndex> tsNtSetmealIndex = (List<TsNtSetmealIndex>) MemcachedUtils.get(MemcachedKey.tsNtSetmealIndex);
            if (tsNtSetmealIndex != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("setmeals", tsNtSetmealIndex);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取可购买套餐列表成功");
                retInfo.setObj(map);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂时没有套餐");
            }

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 简介
     * @version 1.0
     * @time 2015-12-28
     * @author lizhun
     */
    @RequestMapping(value = "/setmeal/detail", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView detail(@RequestParam(value = "set_id", required = true) String arti_id) {
        //打印入参
        logger.info(Constants.prefix_in + "arti_id=" + arti_id);
        ModelAndView mv = new ModelAndView("/app/article/detail");
        try {
            Map<String, String> tsNtSetmealSynopisMap = (Map<String, String>) MemcachedUtils.get(MemcachedKey.tsNtSetmealSynopisMap);
            mv.addObject("content", tsNtSetmealSynopisMap.get(arti_id));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(tsNtSetmealSynopisMap.get(arti_id)));
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
        }
        return mv;
    }
}


